package com.sunbird.crm.servce.impl;

import com.sunbird.crm.dao.sys.TUserMapper;
import com.sunbird.crm.entity.sys.TUser;
import com.sunbird.crm.servce.SysUserService;
import com.sunbird.crm.dao.sys.TRolePermissionMapper;
import com.sunbird.crm.dao.sys.TUserRoleMapper;
import com.sunbird.crm.base.utils.MD5DES;
import org.apache.shiro.util.Assert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * @ClassName SysUserServiceImpl
 * @Descriptiom TODO
 * @Author Sunbird
 * @Date 2019/8/20 10:09
 * @Version 1.0
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private TUserRoleMapper userRoleMapper;
    @Autowired
    private TRolePermissionMapper rolePermissionMapper;
    @Autowired
    private TUserMapper tUserMapper ;



    /**
     * @Author Sunbird
     * @Description 查询用户登陆
     * @Date  2019/8/24
     * @Param
     * @return
     **/

    @Override
    @Transactional
    public TUser getUser(TUser user) {
        TUser user1 =tUserMapper.selectByPrimaryKey(user.getUserCode());
        if (user1 ==null){
            return  null;
        }
        //判断密码是否正确
        Assert.state(StringUtils.equals(user1.getPassword(),user.getPassword()),"密码错误");
        user1.setLastLoginDate(Calendar.getInstance().getTime());
//        user1.setLastLoginIp(Calendar);
        tUserMapper.updateByPrimaryKey(user1);
        return user1;
    }

        /**
         * @Author Sunbird
         * @Description 根据角色id 查询权限
         * @Date  2019/8/21
         * @Param
         * @return
         **/
    @Override
    public Set<String> findPermissionsByUserRoleId(Integer userId) {
//        rolePermissionMapper.selectByRoleid(userId);
        return null;
    }

    /**
     * @Author Sunbird
     * @Description 新增账号
     * @Date  2019/8/24
     * @Param
     * @return
     **/
    @Override
    @Transactional
    public void save(TUser user) {
        try {
            user.setPassword(MD5DES.encrypt(user.getPassword()));
            user.setUpdateDate(new Date());
            user.setCreateDate(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        tUserMapper.insert(user);
        //新增用户角色

    }
}
